#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "./hmmer-3.3.2.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then echo "请输入正确的构建路径"; exit 1; fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then echo "请输入正确的安装路径"; exit 1; fi
[[ "$1" = "$2" ]] && echo "构建路径与安装路径不能相同" && exit 1
buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi

wget -t 10 -c -P $buildpath http://eddylab.org/software/hmmer/hmmer-3.3.2.tar.gz
cd $buildpath
tar xf hmmer-3.3.2.tar.gz
cd hmmer-3.3.2

#use sse2neon to adapt arm
wget -t 10 -c https://github.com/DLTcollab/sse2neon/archive/refs/tags/v1.5.1.tar.gz
tar xf v1.5.1.tar.gz
mv sse2neon-1.5.1 sse2neon
sed -i '5551c #include "sse2neon/sse2neon.h"' configure

./configure --prefix=$installpath/hmmer enable_sse=yes CC=clang CFLAGS="-O3 -march=armv8.2-a+fp+simd+crc -fsigned-char" 
sed -i '209,213d' easel/esl_cpu.c
sed -i -e 's/#include <x86intrin.h>/#include "..\/sse2neon\/sse2neon.h"/' easel/*.h
sed -i -e '41c #include "../sse2neon/sse2neon.h"' src/hmmer.h
sed -i -e '/#include <xmmintrin.h>/d' \
-e '/#include <emmintrin.h>/d' \
-e '/#include <x86intrin.h>/d' `find . -name *.c -o -name *.h`
make  -j$(nproc)  && sudo make install && make clean
cat>"$installpath/hmmer/hmmer_modulefiles"<<EOF
#%Module1.0
conflict hmmer
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set hmmer \$pwd
setenv hmmer \$hmmer
prepend-path PATH \$hmmer/bin
EOF
